package org.phalanx.client.dialogs.servernode;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.IOException;

import javax.swing.JPanel;

import org.phalanx.client.PhalanxClient;
import org.phalanx.client.UI.PhalanxIconButton;
import org.phalanx.client.UI.PhalanxPanelManager;
import org.phalanx.client.UI.swing.listeners.pButtonListener;
import org.phalanx.client.UI.swing.listeners.pButtonPressed;
import org.phalanx.client.dialogs.MainMenuPanel;
import org.phalanx.client.dialogs.PhalanxWindow;
import org.phalanx.client.net.PhalanxConnectionManager;


public class ServerNodeConnectionPanel extends JPanel implements pButtonListener {
	public static final String NAME = "SERVERNODEPANEL";
	private PhalanxWindow masterFrame;
	private ServerNodeConnectionFieldPanel textInput;
	private PhalanxServerNodeListPanel listPanel;
	private PhalanxServerNodeNavPanel navPanel;
	
	public ServerNodeConnectionPanel(PhalanxWindow parentFrame){
		this.masterFrame = parentFrame;
		this.initializePanel();
		setUpPanels();
		this.setUpConnectionFieldGrid();
		this.setUpConnectionListGrid();
		this.setUpListInfoPanel();
		this.setUpBottomNavigationPanel();
	}
	
	private void initializePanel(){
		this.setBackground(Color.black);
		this.setLayout(new GridBagLayout());
	}
	
	private void setUpPanels(){
		textInput = new ServerNodeConnectionFieldPanel(this);
		listPanel = new PhalanxServerNodeListPanel(this);
		navPanel = new PhalanxServerNodeNavPanel(this);
	}
	
	private void setUpConnectionFieldGrid(){
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.BOTH;
		
		this.add(textInput, c);
	}
	
	private void setUpConnectionListGrid(){
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 1;
		c.weightx = 0.5;
		c.weighty = 0.5;
		c.fill = GridBagConstraints.BOTH;
		
		this.add(listPanel, c);
	}
	
	private void setUpListInfoPanel(){
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 0;
		c.gridheight = 2;
		c.fill = GridBagConstraints.BOTH;
	}
	
	private void setUpBottomNavigationPanel(){
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 2;
		c.weightx = 0.5;
		c.fill = GridBagConstraints.BOTH;
		
		this.add(navPanel, c);
	}
	
	public void switchToMainMenu(boolean killEditor) {
		if (killEditor) {
			PhalanxPanelManager.getInstance().removePanel(NAME);
		}
		this.masterFrame.replacePanel(PhalanxPanelManager.getInstance().getPanel(MainMenuPanel.NAME));
	}

	public void actionPerformed(pButtonPressed p) {
		if (p.getComponent() instanceof PhalanxIconButton){
			PhalanxIconButton btn = (PhalanxIconButton)p.getComponent();
			
			if (btn.getName().equals("connectnode")){
				connectToServerNode();
			}
		}
		
	}
	
	private void connectToServerNode() {
		try {
			PhalanxConnectionManager pc = PhalanxClient.getClientInstance().getPhalanxNodeConnectionManager();
			pc.setServerNode(textInput.getTextField().getText());
			pc.connectToServerNode();
		} catch (IOException ie) {}
	}
}
